package co2.bean;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.hibernate.id.UUIDHexGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import co2.engine.DAOInterface;
import co2.engine.SessionValidation;
import co2.pojos.BankMotivasi;
import co2.pojos.HasilMotivasi;
import co2.pojos.MstUser;

@ManagedBean
@ViewScoped
public class UserMotivasiBean extends AbstractBean implements Serializable {
	private static final long serialVersionUID = 1L;

	private List<BankMotivasi> listbs;
	private MstUser user;
	private Map sessionMap;
	
	@Autowired
	@Qualifier("daoI")
	private DAOInterface dao;
	
	private boolean unassign;
	
	private void shuffleList(List list){
		long seed = System.nanoTime();
		Collections.shuffle(list, new Random(seed));
	}
	
	@PostConstruct
	public void postCon(){
		unassign = false;
		SignInBean signInBean = SessionValidation.checkSession();
		
		if(signInBean!=null){
			user = signInBean.getTheUser();
			if(user.getAssignedMotivasi()!=null){
				listbs = dao.getSoalByPaketMotivasi(user.getAssignedMotivasi());
				
			}else{
				unassign=true;
			} 
		}else{
			try{
				String path = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
				FacesContext.getCurrentInstance().getExternalContext().redirect(path+"/jsf/target/tumbal.jsf");
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	
	
	public void submit(){
		for(BankMotivasi bm:listbs){
			try{
				HasilMotivasi hm = new HasilMotivasi();
				hm.setId(new UUIDHexGenerator().generate(null, null).toString().toUpperCase());
				BankMotivasi x = new BankMotivasi();
				x.setId(bm.getId());
				hm.setBankMotivasi(x);
				MstUser y = new MstUser();
				y.setUserId(user.getUserId());
				hm.setMstUser(y);
				hm.setJawaban(bm.getJwbUser());
				dao.save(hm);
				
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
		user.setAssignedMotivasi(null);
		try {
			dao.update(user);
			String requestPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() ;
			FacesContext.getCurrentInstance().getExternalContext().redirect(requestPath+"/jsf/target/motivasi_next.jsf");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	
	
	
	
	
	
	
	
	//getter and setter

	public MstUser getUser() {
		return user;
	}

	public void setUser(MstUser user) {
		this.user = user;
	}

	public boolean isUnassign() {
		return unassign;
	}

	public void setUnassign(boolean unassign) {
		this.unassign = unassign;
	}
	public Map getSessionMap() {
		return sessionMap;
	}

	public void setSessionMap(Map sessionMap) {
		this.sessionMap = sessionMap;
	}

	public DAOInterface getDao() {
		return dao;
	}

	public void setDao(DAOInterface dao) {
		this.dao = dao;
	}

	public List<BankMotivasi> getListbs() {
		return listbs;
	}

	public void setListbs(List<BankMotivasi> listbs) {
		this.listbs = listbs;
	}
}
